//
//  Networkvc.m
//  001--RAC集合类
//
//  Created by macOS on 2017/4/27.
//  Copyright © 2017年 TZ. All rights reserved.
//

#import "Networkvc.h"
#import <ReactiveObjC.h>

#import <RACReturnSignal.h>
@interface Networkvc ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextField *tf;

@property(nonatomic, strong) RACSignal *signal;

@end

@implementation Networkvc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.view.backgroundColor = [UIColor grayColor];
    [self demo10];

}


- (IBAction)dissMiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)demo1 {

    
    RACSignal *sigal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        
        //发送请求
        //请求网络数据
        NSLog(@"请求网络数据 1");
        [subscriber  sendNext:@"网络数据 1 来了"];
        
        return  [RACDisposable disposableWithBlock:^{
            
        }];
    }];
    
    
    RACSignal *sigal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        
        //发送请求
        //请求网络数据
        NSLog(@"请求网络数据 2");
        [subscriber  sendNext:@"网络数据 2 来了"];
        
        return  [RACDisposable disposableWithBlock:^{
            
        }];
    }];
    
    
    
    //信号数组NSArray<RACSignal *>
    //当数组中所有信号都发送了数据 才会 执行Selector
  
    [self rac_liftSelector:@selector(handleSigal1Data:AndSigal2:) withSignalsFromArray:@[sigal1,sigal2]];
    

}
- (void)handleSigal1Data:(NSString *)str1 AndSigal2:(NSString *)str2{
    
    NSLog(@"更新UI%@ ---- %@", str1, str2);
  
}

- (void)demo2 {
  
    
//RAC:给某个对象绑定一个属性
    //RACOberve 监听某个对象的某个属性
    //RACTuplePack: 将数据打包成RACTuple
    //RACTupleUnpack: 将RAC元祖解包
    
//    
//    [_tf.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
//        
//        _label.text = x;
//    }];
    
    RAC(_label,text) = _tf.rac_textSignal;
    

}

- (void)demo3{
    RAC(_label,text) = _tf.rac_textSignal;
    [RACObserve(self.label, text) subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    

}

- (void)demo4{

    
    
    //self 被 signal 引用了
    // signal 也被 self 引用了
    //循环引用
    
    @weakify(self);
    self.signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       
        @strongify(self);
        NSLog(@"%@------强引用一次",self);
        return nil;
    }];
   [self.signal  subscribeNext:^(id  _Nullable x) {
       NSLog(@"%@",x);
   }];

}

- (void)dealloc{
    NSLog(@"-------释放了");
}




- (void)demo5{

    //包装元祖
    RACTuple *typle = RACTuplePack(@{@"name" : @"wsj2012"});
    
    NSLog(@"%@",typle);
    
    RACTupleUnpack(NSDictionary * a) = typle;
    
    NSLog(@"%@", a);
    
}







- (void)demo6 {
  
    RACSignal *sigal  = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        [subscriber sendNext:@"请求网络信号"];
        
        return  nil;
    }];
    
    
//    RACMulticastConnection
    //连接类, 用于当一个信号被多次订阅的时候 避免多次请求调用
    //信号创建里的block
    
    RACMulticastConnection *mc = [sigal publish];
    [mc.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"mc---%@", x);
    }];
    
    [mc.signal subscribeNext:^(id  _Nullable x) {
         NSLog(@"mc---%@", x);
    }];
    [mc connect];
    

}

- (void)demo7 {
 
//    RACCommd 命令
    
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        NSLog(@"input: %@", input);
        RACSignal *sigal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           
            [subscriber sendNext:@"网络清数据内容"];
            
            return  nil;
        }];
        
        return  sigal;
    
    }];
    
    
    
    
   //先订阅  再 执行命令
    //一般不会这么用
   [command.executionSignals subscribeNext:^(RACSignal   * _Nullable  x) {
       
       //x  是信号源 就是上面 block 里的 sigal
       //
       [x subscribeNext:^(id  _Nullable x) {
           //
       }];
   }];
    
    //命令执行
    //先执行 再 订阅
   [[command execute:@"执行命令的参数"]  subscribeNext:^(id  _Nullable x) {
       NSLog(@"收到的数据:%@", x);
   }] ;
    
    
    //switchToLatest
    //获取最新发送的信号
    //这里其实就是 block 里的 sigal
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog( @"%@", x);
    }];
   
    
    
}

- (void)demo8 {

 
    //创建多个信号
    
    
    RACSubject *signalOfSignal = [RACSubject subject];
    
     RACSubject *signal1 =[RACSubject subject];
     RACSubject *signal2 =[RACSubject subject];
     RACSubject *signal3 =[RACSubject subject];
  
//    
//    [signalOfSignal subscribeNext:^(id  _Nullable x) {
//        
//        [x subscribeNext:^(id  _Nullable x) {
//            NSLog(@"%@",x);
//        }];
//        
//    }];
    
    
    
    [signalOfSignal.switchToLatest subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"%@",x );
        
    }];
    
    
    //发送信号
     [signalOfSignal sendNext:signal1];
     [signalOfSignal sendNext:signal2];
     [signalOfSignal sendNext:signal3];
    
    //发送数据
     [signal3 sendNext:@"3"];
     [signal2 sendNext:@"2"];
     [signal1 sendNext:@"1"];
    
    
}

- (void)demo9 {
    
    
    //管理整个事件的 流程
    
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        NSLog(@"input: %@", input);
        RACSignal *sigal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [subscriber sendNext:@"网络清数据内容"];
            //告诉命令信号  信号完毕
            [subscriber sendCompleted];
            return  nil;
        }];
        
        return  sigal;
        
    }];
    
    
    
    
    
    [command.executing subscribeNext:^(NSNumber * _Nullable x) {
        NSLog(@"comand状态%@", x);
    }];
    
    //命令执行
    //先执行 再 订阅
    [[command execute:@"执行命令的参数"]  subscribeNext:^(id  _Nullable x) {
        NSLog(@"收到的数据:%@", x);
    }] ;
    
    
    //switchToLatest
    //获取最新发送的信号
    //这里其实就是 block 里的 sigal
//    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
//        NSLog( @"%@", x);
//    }];
    

   
    
    
}

- (void)demo10 {
 
//    bind 绑定
    
    RACSubject *sub = [RACSubject subject];
    
     //绑定信号
    RACSignal *bindedSignal = [sub bind:^RACSignalBindBlock _Nonnull{
        
        return ^RACSignal * (id value, BOOL *stop){
           //value 原信号发送的内容  就会调用bindBlock
            //这里可以处理源信号 再返回
            return [RACReturnSignal return:value];
        };
        
        }];
    
   [bindedSignal subscribeNext:^(id  _Nullable x) {
       NSLog(@"绑定接收到数据%@",x );
   }];
    
    [sub sendNext:@"发送原始数据"];


}


@end
